<?php
namespace app\admin\controller;

use think\Controller;
use app\common\model\User;
use app\common\model\Video;
use app\common\model\Release;

class Statistics extends Controller
{
    /**
     * 获取统计概览
     * @return json
     */
    public function overview()
    {
        $data = [
            'user_count' => User::count(),
            'video_count' => Video::count(),
            'release_count' => Release::count(),
            'today_user' => User::whereTime('create_time', 'today')->count(),
            'today_video' => Video::whereTime('create_time', 'today')->count(),
            'today_release' => Release::whereTime('create_time', 'today')->count()
        ];

        return json(['code' => 1, 'data' => $data]);
    }

    /**
     * 获取趋势数据
     * @param string $type 统计类型
     * @param string $start_date 开始日期
     * @param string $end_date 结束日期
     * @return json
     */
    public function trend()
    {
        $type = input('type/s', 'user');
        $start_date = input('start_date/s', date('Y-m-d', strtotime('-7 days')));
        $end_date = input('end_date/s', date('Y-m-d'));

        switch ($type) {
            case 'user':
                $model = new User();
                break;
            case 'video':
                $model = new Video();
                break;
            case 'release':
                $model = new Release();
                break;
            default:
                return json(['code' => 0, 'msg' => '统计类型错误']);
        }
        
        $data = $model->field('DATE(create_time) as date, COUNT(*) as count')
            ->whereTime('create_time', 'between', [$start_date, $end_date])
            ->group('DATE(create_time)')
            ->select();

        return json(['code' => 1, 'data' => $data]);
    }
} 